<template>
  <div>
    <div class="page-form">
      <el-select
        v-hasRole="['admin']"
        v-model="queryParams.schoolId"
        placeholder="全部学校"
        clearable
        @change="
          queryParams.className = '';
          handleSearch();
        "
      >
        <el-option
          v-for="dict in xxlb"
          :key="dict.id"
          :label="dict.schoolName"
          :value="dict.id"
        />
      </el-select>
      <el-select
        v-model="queryParams.className"
        placeholder="班级"
        clearable
        filterable
        @change="handleSearch"
      >
        <el-option
          v-for="dict in queryParamsBjlb"
          :key="dict.id"
          :label="dict.className"
          :value="dict.className"
        />
      </el-select>
      <el-select
        v-model="queryParams.selectSubject"
        placeholder="全部考试"
        clearable
        @change="handleSearch"
      >
        <el-option
          v-for="dict in kslb"
          :key="dict.id"
          :label="dict.name"
          :value="dict.id"
        />
      </el-select>
      <el-input
        v-model="queryParams.admissionNo"
        placeholder="准考证号"
        clearable
        @input="handleSearch"
      />
      <el-input
        v-model="queryParams.studentName"
        placeholder="考生姓名"
        clearable
        @input="handleSearch"
      />
    </div>
    <el-table
      stripe
      border
      ref="tableList"
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      row-key="id"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        :reserve-selection="true"
      />
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="ID"
        align="center"
        prop="id"
        :show-overflow-tooltip="true"
        width="80"
      />
      <el-table-column
        label="姓名"
        align="center"
        prop="studentName"
        :show-overflow-tooltip="true"
        width="120"
      />
      <el-table-column
        label="所属学校"
        align="center"
        prop="schoolName"
        :show-overflow-tooltip="true"
        width="120"
      />
      <el-table-column
        label="学号"
        align="center"
        prop="studentNo"
        :show-overflow-tooltip="true"
        width="140"
      />
      <el-table-column
        label="准考证号"
        align="center"
        prop="admissionNo"
        :show-overflow-tooltip="true"
        width="140"
      />
      <!-- <el-table-column
        label="考场号"
        align="center"
        prop="examRoomNo"
        :show-overflow-tooltip="true"
        width="140"
      />
      <el-table-column
        label="座位号"
        align="center"
        prop="seatNo"
        :show-overflow-tooltip="true"
        width="100"
      /> -->
      <el-table-column
        label="班级"
        align="center"
        prop="className"
        :show-overflow-tooltip="true"
        width="140"
      />
      <el-table-column label="选科" align="center">
        <template slot-scope="scope">
          <km-tag :option="xkFilter(scope.row.selectSubjectList)"></km-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="小语种"
        align="center"
        :show-overflow-tooltip="true"
        width="100"
      >
        <template slot-scope="scope">
          <km-tag :option="scope.row.minorLanguage"></km-tag>
        </template>
      </el-table-column> -->

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120"
      >
        <template slot-scope="scope">
          <el-link type="primary" @click="edit(scope.row)">编辑</el-link>
          <el-link
            type="danger"
            v-if="$auth.hasRole('admin')"
            :disabled="!scanStatus"
            @click="del(scope.row.id)"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="page-bottom">
      <div class="sel-box">
        <p>
          已选 <span>{{ ids.length }}</span> 项 / 共 {{ total }} 项
        </p>
        <el-button type="primary" @click="edit(false)" v-hasRole="['admin']"
          >单个添加</el-button
        >
        <el-button
          type="primary"
          @click="upload.open = true"
          v-hasRole="['admin']"
          >批量导入</el-button
        >
        <el-button
          type="danger"
          :disabled="multiple || !scanStatus"
          @click="del(false)"
          v-hasRole="['admin']"
          >批量删除</el-button
        >
        <el-button
          type="danger"
          :disabled="total == 0 || !scanStatus"

          @click="qbsc"
          v-hasRole="['admin']"
          >全部删除</el-button
        >
      </div>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <el-dialog
      ref="editDialog"
      class="editKmVisible"
      title="创建/编辑学生"
      :visible.sync="editKmVisible"
      width="400px"
      :before-close="beforeClose"
    >
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="rules"
        label-position="left"
        label-width="100px"
      >
        <el-form-item label="学校名称" prop="schoolId">
          <el-select
            :disabled="!this.$auth.hasRole('admin')"
            v-model="editForm.schoolId"
            filterable
            @change="schoolChange"
          >
            <el-option
              v-for="dict in xxlb"
              :key="dict.schoolId"
              :label="dict.schoolName"
              :value="dict.schoolId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="年级" prop="gradeId">
          <el-select v-model="editForm.gradeId">
            <el-option
              v-for="dict in njlb"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="班级" prop="classId">
          <el-select v-model="editForm.classId" filterable>
            <el-option
              v-for="dict in bjlb"
              :key="dict.id"
              :label="dict.className"
              :value="dict.id.toString()"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="学生姓名" prop="studentName">
          <el-input
            v-model="editForm.studentName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="学号" prop="studentNo">
          <el-input v-model="editForm.studentNo" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="准考证号" prop="admissionNo">
          <el-input
            v-model="editForm.admissionNo"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="考场号" prop="examRoomNo">
          <el-input v-model="editForm.examRoomNo" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="座位号" prop="seatNo">
          <el-input v-model="editForm.seatNo" autocomplete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item label="考场座位号" v-if="(editForm.roomNoAndSeatNo || []).length === 0">
           <el-col :span="12" >
            <el-form-item label="" prop="examRoomNo" style="width:96%">
              <el-input v-model="editForm.examRoomNo" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" prop="seatNo" style="width:96%">
              <el-input v-model="editForm.seatNo" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6" style="text-align:center;">
            <span class="examAdd" @click="addExam">动态考场</span>
          </el-col>
        </el-form-item> -->
        <!-- <el-form-item
        v-else
         label-width="0"
          prop="roomNoAndSeatNo"
          :rules="examSeatRules.roomNoAndSeatNo">
          <div
            v-for="(item,index) in editForm.roomNoAndSeatNo"
            :key="index"
            style="display:flex;align-items:center;margin-bottom:10px;">
            <el-form-item
             label-width="0"
              :prop="`roomNoAndSeatNo.${index}.subjectName`"
              :rules="examSeatRules.subjectName"
              style="width:100px;">
              <el-select v-model="item.subjectName" style="width:100px;">
                <el-option
                  v-for="d in xklb"
                  :key="d.subjectName"
                  :label="d.subjectName"
                  :value="d.subjectName"/>
              </el-select>
            </el-form-item>
            <b style="margin:0 6px;">考座号</b>
            <el-form-item
             label-width="0"
              :prop="`roomNoAndSeatNo.${index}.examRoomNo`"
              :rules="examSeatRules.examRoomNo"
              style="width:120px;">
              <el-input v-model="item.examRoomNo" />
            </el-form-item>
            <el-form-item
             label-width="0"
              :prop="`roomNoAndSeatNo.${index}.seatNo`"
              :rules="examSeatRules.seatNo"
              style="width:120px;margin-left:6px;">
              <el-input v-model="item.seatNo" />
            </el-form-item>
            <div style="margin-left:5px;">
              <span
                v-if="index===editForm.roomNoAndSeatNo.length-1"
                class="examAdd"
                @click="addExam">动态考场</span>
              <i
                class="el-icon-delete"
                style="color:#f56c6c;margin-left:4px;cursor:pointer;"
                @click="deletedtkc(index)"/>
            </div>
          </div>
        </el-form-item> -->
        <!-- <div style="display:flex;align-items:center;margin-bottom:20px;" v-else v-for="(item,index) in roomNoAndSeatNo" :key="item.subjectName">
            <div style="display:flex;align-items:center;width:218px;">
                <el-select style="width:90px;" v-model="item.subjectName">
                  <el-option
                    v-for="dict in xklb"
                    :key="dict.subjectName"
                    :label="dict.subjectName"
                    :value="dict.subjectName"
                  />
              </el-select>
              <b style="margin-left:5px;">考座号</b>
            </div>
            <el-col :span="9" >
                <el-input v-model="item.examRoomNo" autocomplete="off" style="width:95%"></el-input>
            </el-col>
            <el-col :span="9">
                <el-input v-model="item.seatNo" autocomplete="off" style="width:95%"></el-input>
            </el-col>
            <el-col :span="6" style="text-align:center;">
              <span class="examAdd" @click="addExam" v-if="index==roomNoAndSeatNo.length-1">动态考场</span>
              <i class="el-icon-delete" @click="deletedtkc(index)" style="display:inline-block;margin-left:5px;cursor:pointer;"></i>
            </el-col>
        </div> -->
        <el-form-item label="选科" prop="selectSubjectList">
          <el-select v-model="editForm.selectSubjectList" multiple>
            <el-option
              v-for="dict in xklb"
              :key="dict.subjectId"
              :label="dict.subjectName"
              :value="dict.subjectId"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="小语种" prop="minorLanguage">
          <el-select v-model="editForm.minorLanguage">
            <el-option
              v-for="dict in xklb"
              :key="dict.subjectId"
              :label="dict.subjectName"
              :value="dict.subjectName"
            />
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">返 回</el-button>
        <el-button type="primary" @click="editSubmit" :loading="loading"
          >保 存</el-button
        >
      </div>
    </el-dialog>

    <el-dialog
      title="批量上传-考生"
      :visible.sync="upload.open"
      :close-on-click-modal="false"
      width="400px"
      append-to-body
    >
      <span style="display:block;padding-bottom:15px;font-size:12px;color:red;">注意:重复导入时，原有数据将被新导入数据覆盖</span>
      <div style="display:flex;align-items:center;">
          <div style="margin-bottom:15px;margin-right:15px;">导入学校</div>
          <el-select
          v-model="upload.deptId"
          filterable
          style="margin-bottom: 15px"
          @change="getExamSchoolId"
          ref="schoolSelect"
        >
          <el-option
            v-for="dict in xxlb"
            :key="dict.schoolId"
            :label="dict.schoolName"
            :value="dict.schoolId"
          />
        </el-select>
      </div>
      <!-- <el-select
        v-model="upload.status"
        filterable
        style="margin-bottom: 15px"
        @change="getExamSchoolId"
      >
        <el-option label="同步" value="0" />
        <el-option label="不同步" value="1" />
      </el-select> -->
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        action=""
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        :http-request="customUpload"
        drag
        @dragover.native="handleDragOver"
        @drop.native="handleDrop"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="submitFileForm"
          :loading="upload.isUploading"
          >确 定</el-button
        >
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { blobValidate } from "@/utils/exam";
import { saveAs } from 'file-saver'
import { debounce } from "lodash";
import { subjectpage } from "@/api/xmgl/ksgl";
import {
  examStudentList,
  examStudentAdd,
  examStudentEdit,
  examStudentDel,
  classlist,
  delFoExamStudent,
  uploadImport
} from "@/api/xmgl/xsgl";
import { scanStatus } from "@/api/jcgl/jsgl";

import { getToken } from "@/utils/auth";
import { schoollist } from "@/api/xmgl/csxx";
import { checkPassword, schoolsubjectlist } from "@/api/common";
export default {
  props: {
    xmxx: {},
  },
  dicts: ["grade"],
  data() {
    return {
      scanStatus: false,
      loading: false,

      ids: [],

      total: 0,
      tableData: [],

      multiple: true,

      queryParams: {
        pageNum: 1,
        pageSize: 15,
      },

      editKmVisible: false,
      editForm: {
        schoolId: "",
        gradeId: "",
        classId: "",
        studentName: "",
        studentNo: "",
        admissionNo: "",
        examRoomNo: "",
        seatNo: "",
        selectSubjectList: [],
        roomNoAndSeatNo: []
      },
       // 统一写校验规则
        examSeatRules: {
          roomNoAndSeatNo: [
            {
              validator: (rule, value, callback) => {
                if (!value || value.length === 0) {
                  callback(new Error('至少填写一个考场座位号'));
                } else {
                  callback();
                }
              },
              trigger: ['blur', 'change']
            }
          ],
          subjectName: [{ required: true, message: "请选择学科", trigger: "change" }],
          examRoomNo: [{ required: true, message: "请输入考场号", trigger: "change" }],
          seatNo: [{ required: true, message: "请输入座位号", trigger: "change" }]
        },
      rules: {
        schoolId: [
          { required: true, message: "学校名称不能为空", trigger: "change" },
        ],
        gradeId: [
          { required: true, message: "年级不能为空", trigger: "change" },
        ],
        // classId: [
        //   { required: true, message: "班级不能为空", trigger: "change" },
        // ],
        studentName: [
          { required: true, message: "学生姓名不能为空", trigger: "blur" },
        ],
        studentNo: [
          { required: true, message: "学号不能为空", trigger: "blur" },
        ],
        admissionNo: [
          { required: true, message: "准考证号不能为空", trigger: "blur" },
        ],
        examRoomNo: [
          { required: true, message: "考场号不能为空", trigger: "blur" },
        ],
        seatNo: [
          { required: true, message: "座位号不能为空", trigger: "blur" },
        ],
        selectSubjectList: [
          { required: true, message: "选科不能为空", trigger: "change" },
        ],
        // minorLanguage: [
        //   { required: true, message: "小语种不能为空", trigger: "change" },
        // ],
      },

      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 学校
        deptId: "",
        examSchoolId: "",
        // 同步
        status: "1",
        // 设置上传的请求头部
        headers: {
          Authorization: "Bearer " + getToken()
        },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/exam/student/import",
      },

      qbbjlb: [],
      xxlb: [],
      bjlb: [],
      xklb: [],
      kslb: [],
      subjectList:['语文','数学','英语','物理','化学','生物','历史','地理','政治','日语'],
    };
  },
  computed: {
    queryParamsBjlb: function () {
      return this.qbbjlb.filter((item) => {
        return item.schoolId == this.queryParams.schoolId;
      });
    },
    njlb: function () {
      return this.dict.type.grade.filter((item) => {
        return item.value == this.xmxx.grade;
      });
    },
  },
  created() {
    scanStatus({examId: this.xmxx.id}).then((res) => {
      this.scanStatus = res.data;
    });

    subjectpage({ pageNum: 1, pageSize: 9999, examId: this.xmxx.id }).then(
      (response) => {
        this.kslb = response.rows;
      }
    );

    schoollist({ pageNum: 1, pageSize: 9999, examId: this.xmxx.id }).then(
      (res) => {
        this.xxlb = res.rows;

        if (!this.$auth.hasRole("admin")) {
          this.queryParams.schoolId = this.xxlb.filter((item) => {
            return this.$store.state.user.deptId == item.schoolId;
          })[0].id;
        }

        this.getList();
      }
    );

    classlist({
      pageNum: 1,
      pageSize: 9999,
      examId: this.xmxx.id,
    }).then((res) => {
      this.qbbjlb = res.rows;
    });
  },
  methods: {
    getList() {
      this.loading = true;
      this.tableData = [];
      examStudentList({ ...this.queryParams, examId: this.xmxx.id })
        .then((response) => {
          this.tableData = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    debouncedSearch: debounce(function () {
      this.queryParams.pageNum = 1;
      this.getList();
    }, 500),
    handleSearch() {
      this.debouncedSearch();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    edit(row) {
      if (row) {
        this.editForm.id = row.id;

        this.editForm.schoolId = this.xxlb.filter((item) => {
          return item.id == row.schoolId;
        })[0].schoolId;
        this.schoolChange();

        this.editForm.gradeId = row.gradeId;
        this.editForm.classId = row.classId ? row.classId.toString() : "";
        this.editForm.studentName = row.studentName;
        this.editForm.studentNo = row.studentNo;
        this.editForm.admissionNo = row.admissionNo;
        this.editForm.examRoomNo = row.examRoomNo;
        this.editForm.seatNo = row.seatNo;
        this.editForm.selectSubjectList = row.selectSubjectList.map((item) => {
          return item.subjectId;
        });
        this.editForm.roomNoAndSeatNo = row.roomNoAndSeatNo
        // this.editForm.minorLanguage = row.minorLanguage;
      }

      this.editKmVisible = true;
    },

    beforeClose(done) {
      this.editForm = {
        schoolId: "",
        gradeId: "",
        classId: "",
        studentName: "",
        studentNo: "",
        admissionNo: "",
        examRoomNo: "",
        seatNo: "",
        selectSubjectList: [],
        roomNoAndSeatNo:[]
        // minorLanguage: "",
      };
      this.bjlb = [];
      this.$nextTick(() => {
        this.$refs.editForm.clearValidate();
        done();
      });
    },
    handleClose() {
      this.beforeClose(() => {
        this.editKmVisible = false;
      });
    },

    editSubmit() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          let req;
          if (this.editForm.id) {
            req = examStudentEdit;
          } else {
            req = examStudentAdd;
          }
          let data = {
            ...this.editForm,
            examId: this.xmxx.id,
          };

          let schoolData = this.xxlb.filter((item) => {
            return item.schoolId == data.schoolId;
          })[0];

          data.schoolId = schoolData.id;
          data.schoolName = schoolData.schoolName;

          data.grade = this.njlb[0].label;

          data.department =
            data.gradeId <= 6 ? "小学" : data.gradeId <= 10 ? "初中" : "高中";

          if (data.classId) {
            data.className = this.qbbjlb.filter((item) => {
              return item.id == data.classId;
            })[0].className;
          }

          let selectSubjectList = [];
          this.xklb.map((item) => {
            if (data.selectSubjectList.indexOf(item.subjectId) != "-1") {
              let obj = {
                subjectId: item.subjectId,
                examId: this.xmxx.id,
              };
              if (data.id) {
                obj.examStudentId = item.id;
              }
              selectSubjectList.push(obj);
            }
          });

          data.selectSubjectList = selectSubjectList;
          req(data)
            .then((res) => {
              this.$modal.msgSuccess("操作成功");
              this.handleClose();
              this.getList();
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    del(id) {
      this.$prompt(
        "删除考生后，可能造成考生数据异常情况，请输入管理员密码以进行下步操作!",
        "删除考生",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputPlaceholder: "请输入管理员密码",
          inputType: "password",
          inputValidator: (value) => {
            if (!value) {
              return "请输入密码";
            } else {
              return true;
            }
          },
        }
      )
        .then(({ value }) => {
          checkPassword({
            password: value,
            schoolId: this.$store.state.user.deptId,
          }).then((res) => {
            examStudentDel(id ? id : this.ids).then((res) => {
              this.$modal.msgSuccess("操作成功");
              this.$refs.tableList.clearSelection();
              this.ids = [];
              this.getList();
            });
          });
        })
        .catch(() => {});
    },
    qbsc(id) {
      this.$prompt(
        "删除考生后，可能造成考生数据异常情况，请输入管理员密码以进行下步操作!",
        "删除全部考生",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputPlaceholder: "请输入管理员密码",
          inputType: "password",
          inputValidator: (value) => {
            if (!value) {
              return "请输入密码";
            } else {
              return true;
            }
          },
        }
      )
        .then(({ value }) => {
          this.loading = true;
          checkPassword({
            password: value,
            schoolId: this.$store.state.user.deptId,
          })
            .then((res) => {
              delFoExamStudent({
                ...this.queryParams,
                examId: this.xmxx.id,
              }).then((res) => {
                this.$modal.msgSuccess("操作成功");
                this.$refs.tableList.clearSelection();
                this.ids = [];
                this.queryParams.pageNum = 1;
                this.getList();
              });
            })
            .finally(() => {
              this.loading = false;
            });
        })
        .catch(() => {});
    },

    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      window.open(
        "https://bs-exam-bucket.oss-cn-beijing.aliyuncs.com/dev/bsta/exam/uploadPath/upload/template/student_template.xlsx"
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },

    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.deptId = "";
      this.upload.status = "1";
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      // this.$alert(
      //   "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
      //     response.msg +
      //     "</div>",
      //   "导入结果",
      //   { dangerouslyUseHTMLString: true }
      // );
      this.allDeptList = null;
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      if (!this.upload.deptId) {
        this.$modal.alertError("请选择导入学校");
        return;
      }
      this.$refs.upload.submit();
     
    },

    schoolChange() {
      this.editForm.classId = "";
      let examSchoolId = this.xxlb.filter((item) => {
        return item.schoolId == this.editForm.schoolId;
      })[0].id;
      this.bjlb = this.qbbjlb.filter((item) => {
        return item.schoolId == examSchoolId;
      });

      schoolsubjectlist({
        examId: this.xmxx.id,
        examSchoolId: examSchoolId,
      }).then((res) => {
        this.xklb = res.rows;
      });
    },

    xkFilter(row) {
      return row.map((item) => {
        return item.subjectName;
      });
    },
    getExamSchoolId(value) {
      this.upload.examSchoolId = this.xxlb.filter((item) => {
        return item.schoolId == value;
      })[0].id;
    },
    // 3. 拖拽经过上传区域时，关闭el-select下拉框
    handleDragOver() {
      this.closeSelectDropdown();
    },
    // 4. 拖拽释放到上传区域时，关闭el-select下拉框
    handleDrop() {
      this.closeSelectDropdown();
    },
    // 封装关闭下拉框的方法
    closeSelectDropdown() {
      // 通过ref获取el-select实例，调用其内部方法关闭下拉框
      if (this.$refs.schoolSelect) {
        this.$refs.schoolSelect.visible = false; // 强制关闭下拉框
      }
    },
    customUpload(option){
      this.upload.isUploading = true;
        let data={
          file:option.file
        }
        uploadImport(data,this.upload.deptId,this.xmxx.id,this.upload.examSchoolId,this.upload.status).then(res=>{
          console.info('dddd',res)
          if(res&&res.data&&res.data.size!=0){
            // 直接使用response.data（已为blob类型）
            const blob = new Blob([res.data]);
            // 获取文件名
            const filename = "考生导入结果.xlsx";
            saveAs(blob, filename)
            this.upload.open = false;
            this.upload.deptId = "";
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.allDeptList = null;
            this.getList();
            this.$alert('失败原因及原始表格已下载，请修改后再重新导入', '导入失败!', {
              confirmButtonText: '确定',
               customClass: 'my-alert',
              callback: action => {}
            });
          }else{
            this.upload.open = false;
            this.upload.deptId = "";
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$message({
              duration:3000,
              message: '导入成功！',
              type: 'success'
            });
            this.allDeptList = null;
            classlist({
              pageNum: 1,
              pageSize: 9999,
              examId: this.xmxx.id,
            }).then((res) => {
              this.qbbjlb = res.rows;
            });
            this.getList();
          }
        })
    },
    // 动态考场
    addExam(){
      (this.editForm.roomNoAndSeatNo ||= []).push({
        subjectName: '',
        examRoomNo: '',
        seatNo: '',
        subjectId:''
      });
    },
    deletedtkc(index){
       this.editForm.roomNoAndSeatNo.splice(index, 1);
        // 删完手动触发一次数组级校验
        this.$nextTick(() => {
          this.$refs.editForm.validateField('roomNoAndSeatNo');
        });
    }
  },
};
</script>

<style lang="scss" scoped>
.editKmVisible {
  .el-cascader,
  .el-select,.el-input {
    width:260px;
  }
  // .el-input{
  //   width:100%;
  // }
  .examAdd{
    color:#1A48DB;
    cursor: pointer;
  }
}

</style>
<style>
.my-alert .el-message-box__message p {
  color: red;
}
</style>